package com.simen.hellobytes.android;

import android.app.ActivityManager;
import android.content.Context;
import com.simen.hellobytes.lang.ListUtil;

import java.util.List;

/**
 * @author Created by SimenHi.
 * @date 创建日期 2019/1/14 10:54
 * @modify 修改者 SimenHi
 */
public class SystemUtil {

    public static boolean isCurrentProcessName(Context context, String processName) {
        if (context == null) {
            return false;
        }

        return processName.equals(getProcessName(context));
    }

    public static String getProcessName(Context context) {
        if (context == null) {
            return null;
        }

        int pid = android.os.Process.myPid();
        ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> processInfoList = manager.getRunningAppProcesses();
        if (ListUtil.isEmpty(processInfoList)) {
            return null;
        }

        for (ActivityManager.RunningAppProcessInfo processInfo : processInfoList) {
            if (processInfo != null && processInfo.pid == pid) {
                return processInfo.processName;
            }
        }
        return null;
    }

}
